package com.itheima.interceptor;

import com.alibaba.excel.event.Handler;
import com.itheima.utils.JwtUtils;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import java.util.Objects;

@Component
public class LoginInterceptor implements HandlerInterceptor {

    /**
     * 用户登录拦截器
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //1)获取请求头中的令牌
        String token =request.getHeader("token");
        //2)如果令牌为空,或者解析失败,则拦截
        if (Objects.isNull(token)|| !JwtUtils.verfiy(token)){
            response.setStatus(401);
            return false;
        }
        return true;
    }



}
